<template>
	<view class="news">
		

		<div class="searchbox">
			<div class="bgi">
				<image class="pic" src="/static/14.png" alt=""/>
				<uv-navbar bgColor='transparent' :placeholder='true' @leftClick='goBack'>
					<template v-slot:left  >
						<view class="title" ><uv-icon bold	 class="return_icon" name="arrow-left"></uv-icon></view>
					</template>
					<template v-slot:center>
						<view class="title">健康资讯</view>
					</template>
				</uv-navbar>
				<div class="search" @click='navTo("/pages/search/search")'>
					<image class="icon" src="/static/search.png" mode=""></image>
					<input class="text" type="text" @confirm='confirm' confirm-type='search' placeholder="搜索资讯关键词">
				</div>
			</div>
			
			
			
		</div>
		
		<div class="tabs">
			<div class="item" @click='tabSwicth(index)' v-for="(item,index) in tabs" :key='index'>
				<div :class="['name',currentTab==index && 'active']">{{item.name}}</div>
			</div>
		</div>
		
		<div class="list_box" v-if='loading'>
			<div class="item" @click='toDetail(item.id)' v-for="(item,index) in data.list" :key="index">
				<image class="pic"  :src="baseURL+item.image" mode=""></image>
				<div class="desc">{{item.title}}</div>
			</div>
			<div class="more_bar" @click='lookMore'>{{tips}}</div>
		</div>
		
		<div class="empty"  v-if='data.list.length==0 && loading'>没有数据</div>
	</view>
</template>

<script>
	import { gettype,getnewList } from '@/network/api.js'
	import { baseURL } from '@/network/config.js'
	export default {
		data() {
			return {
				currentTab:0,
				tabs:[],
				// tabs:[{name:"健康科普"},{name:"健康生活"},{name:"学术文章"}]
				data:{
					total:0,
					list:[]
				},
				baseURL,
				query:{ page:1,limit:10},
				tips:'查看更多',
				loading:false,
				isNews:false,
			}
		},
		onLoad(options){
			console.log('options:',options)
			let pages = getCurrentPages(); //当前页面栈
			console.log('pages:',pages)
			if(pages.length==1){
				this.isNews = true
			}
			this.getTypeData()
		},
		methods: {
			tabSwicth(index){
				this.tips = '查看更多'
				this.data.list = []
				this.query.page = 1
				this.currentTab = index
				// this.getList(this.tabs[index].id)
				this.getList({newTypeId:this.tabs[index].id,...this.query})
			},
			navTo(path){uni.navigateTo({url: path});},
			async getTypeData(){
				const {data} = await gettype()
				console.log('类型：',data)
				this.tabs = data || []
				// this.getList(this.tabs[0].id)
				this.getList({newTypeId:this.tabs[0].id,...this.query})
			},
			async getList(params,bool){
				if(!bool){
					this.loading = false
				}
				uni.showLoading({title: '加载中'});
				// const { data } = await getnewList({newTypeId})
				const { data } = await getnewList(params)
				uni.hideLoading();
				this.loading = true
				console.log('list:',data)
				// this.data = data || {total:0,list:[]}
				this.data.list = [...this.data.list,...data.list]
				if(data.list.length == 0){
					this.tips = '没有更多了'
					this.query.page --
				}
			},
			toDetail(id){
				uni.navigateTo({
					url:`/pages/chidai_detail/chidai_detail?id=${id}`
				})
			},
			goBack(){
				if(this.isNews){
					uni.reLaunch({url: '/pages/index/index'});
				}else{
					uni.navigateBack({delta: 1});
				}
			},
			lookMore(){
				if(this.tips == '没有更多了'){
					return
				}
				this.query.page ++
				this.getList({newTypeId:this.tabs[this.currentTab].id,...this.query},true)
			},
		}
	}
</script>

<style scoped lang="less">
	.title {
		display: flex;
		align-items: center;
		font-size: 32rpx;
		font-weight: bold;
		color: #363636;
	}
	.return_icon{
		font-weight: bold;
	}
	.bgi {
		position: relative;
		top: 0;
		left: 0;
		right: 0;
		background-color: #ccc;
		// background: url('../../static/14.png') no-repeat center/100% 100%;
		z-index: 99;
		overflow: hidden;
		.pic{
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			width: 100%;
			height: 100%;
			z-index: -99;
		}
	}

	.search {
		height: 70rpx;
		background-color: #fff;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		flex: 1;
		padding: 0 30rpx;
		margin: 10rpx 25rpx 25rpx 25rpx;
		.icon {
			width: 35rpx;
			height: 35rpx;
			margin-right: 10rpx;
		}

		.text {
			flex: 1;
			font-size: 25rpx;
			font-weight: 400;
			color: #898989;
		}
	}
	.tabs{
		display: flex;
		align-items: center;
		height: 60rpx;
		margin: 30rpx 0;
		.item{
			flex:1;
			display: flex;
			justify-content: center;
			align-items: center;
			.name{
				font-size: 28rpx;
				font-weight: 400;
				color: #828282;
				position: relative;
				padding: 0 5rpx;
			}
		}
		.active{
			&::before{
				content: '';
				position: absolute;
				right: 0;
				left: 0;
				bottom:-10rpx;
				height: 5rpx;
				background: #4E6FD9;
				border-radius: 2rpx;
			}
			&.name{
				color:#4E6FD9;
			}
		}
	}
	.list_box{
		border-top:1px solid #EAEAEA;
		margin :20rpx 25rpx 20rpx 25rpx;
		.item{
			display: flex;
			padding: 33rpx 0;
			border-bottom:1px solid #EAEAEA;
			&:last-child{
				border:none;
			}
			.pic{
				width: 340rpx;
				height: 210rpx;
				background: #F2F2F2;
				border-radius: 10rpx;
				flex-shrink: 0;
			}
			.desc{
				font-size: 28rpx;
				font-weight: 400;
				color: #444444;
				line-height: 50rpx;
				flex:1;
				margin: 18rpx 0 0 25rpx;
			}
		}
	}
	.empty{
		color: #444444;
		text-align: center;
	}
	.more_bar{
		font-size: 24rpx;
		font-weight: 400;
		color: #BDBDBD;
		padding: 30rpx 0;
		text-align: center;
	}
</style>